home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / mactotex / cleanps.c < prev    next >
C/C++ Source or Header  |  1991-10-01  |  4KB  |  136 lines

  1. /*
  2.  *    cleanps.c
  3.  *
  4.  *  Program to clean up System 7.0 (LaserPrep 71) (and other) PostScript files.
  5.  *  Current functions:
  6.  *    Delete LaserPrep section (the "ProcSet")
  7.  *    Delete all font definitions (optional)
  8.  *    Delete %%EOF lines
  9.  *    Is wary of very long lines
  10.  *    Breaks long lines at spaces (if possible)
  11.  *  Future Functions:
  12.  *    Delete specific font definitions (optional)
  13.  *    Insert a bounding box
  14.  *    Translate weird (non-printing) characters
  15.  *
  16.  *  Written  08/16/91 asf - (Adam Fedor) fedor@boulder.colorado.edu
  17.  *
  18.  *  Copyright (C) 1991 Adam S. Fedor.  This program may be freely copied
  19.  *    modified or inserted into other programs provided proper credit is
  20.  *    given to the author(s).  There is no expressed or implied warrenty for
  21.  *    this program.
  22.  *
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <fcntl.h>
  27. #include <string.h>
  28.  
  29. #define VERSION        1.0
  30. #define FALSE        0
  31. #define TRUE        ~FALSE
  32. #define MAXARRAY    30
  33. #define MAXLINE        300
  34. #define BeginProc    "%%BeginProcSet"
  35. #define EndProc        "%%EndProcSet"
  36. #define BeginFont    "%%BeginFont"
  37. #define EndFont        "%%EndFont"
  38. #define Eof        "%%EOF"
  39. #define BoundingBox    "%%BoundingBox"
  40.  
  41. FILE                    *outfp, *infp;
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char *argv[];
  46. {
  47. char    ch, *nl, buffer[MAXLINE];
  48. char    *filename, outfile[MAXARRAY], ext[MAXARRAY];
  49. int    count, printing, breakit, cleanfonts;
  50.  
  51.   cleanfonts = FALSE;
  52.   while (--argc > 0 && (*++argv)[0] == '-')
  53.     while (ch = *++argv[0])
  54.       switch (ch) {
  55.       case 'f':
  56.      cleanfonts = TRUE;    
  57.     break;
  58.       case 'h':
  59.     help();
  60.     return 0;
  61.     break;
  62.       default:
  63.     fprintf(stderr, "CLEANPS: unknown option: %c\n", ch);
  64.     help();
  65.     return -1;
  66.     break;
  67.       }
  68.   filename = argv[0];
  69.  
  70.   strcpy(ext, ".clean");
  71.   strcpy(outfile, filename);
  72.   if (! strchr(outfile, '.') == NULL)
  73.     strcpy(strchr(outfile, '.'), ext);
  74.   else
  75.     strcat(outfile, ext);
  76.   if ((infp = fopen(filename, "r")) == NULL) {
  77.     printf ("CLEANPS: can't open %s \n", filename);
  78.     return -1;
  79.   }
  80.   if ((outfp = fopen(outfile, "w")) == NULL) {
  81.     printf ("CLEANPS: can't create output file %s \n", outfile);
  82.     return -1;
  83.   }
  84.  
  85.   /* main loop */
  86.   printing = TRUE;
  87.   breakit = FALSE;
  88.   while ((fgets(buffer, MAXLINE, infp)) != NULL) {
  89.     /* suppress all unwanted lines */
  90.     if (strncmp(buffer, BeginProc, strlen(BeginProc)) == 0)
  91.       printing = FALSE;
  92.     if (strncmp(buffer, EndProc, strlen(EndProc)) == 0) {
  93.       printing = TRUE;
  94.       fgets(buffer, MAXLINE, infp);
  95.     }
  96.     if (cleanfonts && (strncmp(buffer, BeginFont, strlen(BeginFont)) == 0))
  97.       printing = FALSE;
  98.     if (cleanfonts && (strncmp(buffer, EndFont, strlen(EndFont)) == 0)) {
  99.       printing = TRUE;
  100.       /* fputs("bn\n", outfp); */
  101.       fgets(buffer, MAXLINE, infp);
  102.     }
  103.  
  104.     /* if we didn't find a newline, try to break the line */
  105.     if (strchr(buffer, '\n') == NULL) {
  106.       if ((nl = strrchr(buffer, ' ')) != NULL) {
  107.     nl[0] = '\n';
  108.       } else {
  109.     /* can't find a break point, break it anywhere */
  110.         breakit = TRUE; 
  111.       }
  112.     }
  113.       
  114.     if (printing && (strncmp(buffer, Eof, strlen(Eof)) != 0)) {
  115.       fputs(buffer, outfp);
  116.       if (breakit) {
  117.     fputc('\n', outfp);
  118.     printf ("CLEANPS: Line to long - line broken: %s\n", buffer);
  119.       }
  120.     }
  121.     breakit = FALSE;
  122.   } /* while */
  123. } /* main */
  124.  
  125.  
  126. help(){
  127.   fprintf(stderr, "Usage: cleanps [-hf] file\n");
  128.   fprintf(stderr, "  -f  clean out all loaded fonts\n");
  129.   fprintf(stderr, "  -h  print help \n");
  130.   fprintf(stderr, "\n");
  131.   fprintf(stderr, "Output has extention '.clean' \n");  
  132. }
  133.  
  134.  
  135. /********************************** eof *********************************/
  136.